local jieyin = fk.CreateSkill({
  name = "mini_mou__jieyin",
  tags = { Skill.Quest },
})

Fk:loadTranslationTable{
  ["mini_mou__jieyin"] = "结姻",
  [":mini_mou__jieyin"] = "使命技，出牌阶段开始时，你令一名男性角色选择一项："..
  "1.若其有牌，其交给你两张牌（不足则全交给），然后其回复1点体力；"..
  "2.令你失去〖良助〗，然后你视为对其使用一张【顺手牵羊】。<br>\
  ⬤　失败：当你失去〖良助〗时，你回复1点体力并减1点体力上限，然后获得〖枭姬〗。",

  ["#mini_mou__jieyin-choose"] = "结姻：令一名男性角色选择交给你牌或你失去“良助”",
  ["#mini_mou__jieyin-give"] = "结姻：交给 %src 两张牌，你回复1点体力；或点“取消”其失去“良助”",

  ["$mini_mou__jieyin1"] = "谨以白头之约，永结朱陈之好。",
  ["$mini_mou__jieyin2"] = "琴韵谱成同梦语，灯花笑对含羞人。",
}

jieyin:addEffect(fk.EventPhaseStart, {
  anim_type = "control",
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(jieyin.name) and player.phase == Player.Play and
      table.find(player.room.alive_players, function (p)
        return p:isMale()
      end)
  end,
  on_cost = Util.TrueFunc,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local targets = table.filter(room.alive_players, function (p)
      return p:isMale()
    end)
    local to = room:askToChoosePlayers(player, {
      min_num = 1,
      max_num = 1,
      targets = targets,
      skill_name = jieyin.name,
      prompt = "#mini_mou__jieyin-choose",
      cancelable = false,
    })[1]
    room:setPlayerMark(player, "mini_mou__jieyin-phase", to.id)
    if not to:isNude() then
      local min = #to:getCardIds("he") == 1 and 1 or 2
      local cards = room:askToCards(to, {
        min_num = min,
        max_num = 2,
        include_equip = true,
        skill_name = jieyin.name,
        cancelable = player:hasSkill("mini_mou__liangzhu", true),
        prompt = "#mini_mou__jieyin-give:"..player.id,
      })
      if #cards > 0 then
        if to ~= player then
          room:moveCardTo(cards, Card.PlayerHand, player, fk.ReasonGive, jieyin.name, nil, false, to)
        end
        if not to.dead then
          room:recover{
            who = to,
            num = 1,
            recoverBy = player,
            skillName = jieyin.name,
          }
        end
        return
      end
    end
    if not player.dead and player:hasSkill("mini_mou__liangzhu", true) then
      room:handleAddLoseSkills(player, "-mini_mou__liangzhu")
      if not player.dead and not to.dead then
        room:useVirtualCard("snatch", nil, player, to, jieyin.name)
      end
    end
  end,
})

jieyin:addEffect(fk.EventLoseSkill, {
  anim_type = "negative",
  can_trigger = function (self, event, target, player, data)
    return target == player and player:hasSkill(jieyin.name) and
      data.skill:getSkeleton().name == "mini_mou__liangzhu"
  end,
  on_cost = Util.TrueFunc,
  on_use = function (self, event, target, player, data)
    local room = player.room
    room:updateQuestSkillState(player, jieyin.name, true)
    room:setPlayerMark(player, jieyin.name, 0)
    room:invalidateSkill(player, jieyin.name)
    room:recover{
      who = player,
      num = 1,
      recoverBy = player,
      skillName = jieyin.name,
    }
    if player.dead then return end
    room:changeMaxHp(player, -1)
    if player.dead then return end
      room:handleAddLoseSkills(player, "mini_mou__xiaoji")
  end,
})

return jieyin
